import {Component, OnInit, ViewEncapsulation, ViewChild} from '@angular/core';
import {ModalComponent} from "app/shared/components/modal/modal.component";
import {ToolsService} from "app/shared/service/tools/tools.service";
import {Subject} from "rxjs/Subject";
import {ActivatedRoute} from "@angular/router";
import {SeedInfiniteScroll} from "app/shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {AuthService} from "app/shared/service/auth/auth.service";
import {MatchService} from "../match.service";
import {MatchSignFieldResp} from "@interface/index";



@Component({
  selector: 'match-additional',
  templateUrl: './match-additional.component.html',
  styleUrls: ['./match-additional.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class MatchAdditionalComponent implements OnInit {
  public teamUserList: Array<MatchSignFieldResp> = [];
  public addition: Array<MatchSignFieldResp> = [];
  public showModal = false;
  public ngUnsubscribe: Subject<any> = new Subject<any>();
  public params: any = {
    matchId: 0,
    page: 1,
    rows: 10
  };

  @ViewChild(ModalComponent) modal;
  @ViewChild(SeedInfiniteScroll) infiniteScroll;

  constructor(private toolsService: ToolsService,
              private service: MatchService,
              private route: ActivatedRoute,
              private authService: AuthService) {
  }

  /**
   * 参赛人员列表
   */
  public getTeamUserList() {
    this.service.getTeamUserList(this.params).takeUntil(this.ngUnsubscribe).subscribe(res => {
      this.toolsService.hideLoading();
      if (res.result === '0' && res.data && res.data.list.length > 0) {
        this.params['page']++;
        this.infiniteScroll.complete();
        if (res.data.hasNextPage === false) {
          this.infiniteScroll.enable(false);
        }
        this.teamUserList = [...this.teamUserList, ...res.data.list];
      }
    });
  }

  /**
   * 删除参赛人员
   */
  public showDeleteMatchPeople(item) {
    const params = {
      teamSignId: item.id,
      signId: item.signId,
      userId: item.userId
    };

    this.toolsService.present({title: '确定删除？', 'message': `队员：${item.nickName}`, cancelBtn: true}).then(status => {
      if (status) {
        this.service.deleteMatchTeam(params).takeUntil(this.ngUnsubscribe).subscribe(res => {
          if (this.checkResult(res)) {
            this.loadTeamUserList();
          }
        });
      }
    });
  }

  /**
   * 补录团体参赛人员
   */
  public saveForm(item) {
    this.toolsService.showLoading();
    item.matchId = this.params.matchId;
    item.birthday = this.toolsService.formartDate(new Date(item.birthday), 'yyyy/MM/dd');
    item.createTime = this.toolsService.formartDate(new Date(), 'yyyy/MM/dd hh:mm:ss');

    this.service.reAddMatchTeam(item).takeUntil(this.ngUnsubscribe).subscribe(res => {
      this.toolsService.hideLoading();
      if (this.checkResult(res)) {
        this.formSwitch(false);
      }
    });
  }

  /**
   * 获取表单自定义字段
   */
  public getMatchSignField() {
    const params = {
      userId: this.authService.getUserId(),
      matchId: this.params.matchId
    };

    this.service.getMatchSignField(params).takeUntil(this.ngUnsubscribe).subscribe(res => {
      if (this.checkResult(res)) {
        this.addition = res.data.matchSignupFields;
      }
    });
  }

  /**
   * 获取表单开关
   */
  public formSwitch(flag) {
    if (flag) {
      this.modal.show();
      this.showModal = true;
    } else {
      this.loadTeamUserList();
      this.modal.hide();
      this.modal.goBack();
      this.showModal = false;
    }
  }

  /**
   * 获取参赛人员列表
   */
  public loadTeamUserList() {
    this.toolsService.showLoading();
    this.teamUserList = [];
    this.params['page'] = 1;
    this.infiniteScroll.enable(true);
    this.getTeamUserList();
  }

  /**
   *检查返回状态
   */
  public checkResult(res) {
    if (res.result !== '0') {
      this.toolsService.showToast(res.msg);
    }
    return res.result === '0';
  }

  ngOnInit() {
    this.route.queryParams.subscribe(params => {
      this.params.matchId = params['matchId'];
      this.loadTeamUserList();
      this.getMatchSignField();
    });
  }
}
